<!DOCTYPE html>
<html lang="en">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="description" content="elasticSearch  Query DSL(Filters)"/>




  <meta name="keywords" content="elasticsearch," />




  <link rel="alternate" href="/atom.xml" title="imIce">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.4.x" />



<link rel="canonical" href="http://yoursite.com/2017/08/17/elasticsearch_queryDSL/"/>


<meta name="description" content="在一篇中我们实现了mysql数据实时同步至elasticSearch，接下来就是利用java API来实现数据查询，使用Filters方式查询注释写的很清楚，包含分页，关键字查询，范围查询等等">
<meta name="keywords" content="elasticsearch">
<meta property="og:type" content="article">
<meta property="og:title" content="elasticSearch  Query DSL(Filters)">
<meta property="og:url" content="http://yoursite.com/2017/08/17/elasticsearch_queryDSL/index.html">
<meta property="og:site_name" content="imIce">
<meta property="og:description" content="在一篇中我们实现了mysql数据实时同步至elasticSearch，接下来就是利用java API来实现数据查询，使用Filters方式查询注释写的很清楚，包含分页，关键字查询，范围查询等等">
<meta property="og:locale" content="en">
<meta property="og:image" content="https://tuchuang001.com/images/2017/05/09/92385187-file_1493974226877_1421e.jpg">
<meta property="og:updated_time" content="2017-10-12T02:26:26.902Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="elasticSearch  Query DSL(Filters)">
<meta name="twitter:description" content="在一篇中我们实现了mysql数据实时同步至elasticSearch，接下来就是利用java API来实现数据查询，使用Filters方式查询注释写的很清楚，包含分页，关键字查询，范围查询等等">
<meta name="twitter:image" content="https://tuchuang001.com/images/2017/05/09/92385187-file_1493974226877_1421e.jpg">


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.4.x" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />





<script>
  var CONFIG = {
    search: true,
    searchPath: "/search.xml",
    fancybox: true,
    toc: true,
  }
</script>




  



    <title> elasticSearch  Query DSL(Filters) · imIce </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">imIce</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">imIce</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
      
        <li class="menu-search">
          <form>
            <i class="iconfont icon-search" id="open-search"></i>
            <input type="text" class="search-input" id="search-input" />
            <i class="iconfont icon-close" id="close-search"></i>
          </form>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          elasticSearch  Query DSL(Filters)
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          Aug 17, 2017
        </span>
      </div>
    </header>

    
    

    <div class="post-content">
      
        <blockquote>
<p>在一篇中我们实现了mysql数据实时同步至elasticSearch，<br>接下来就是利用<code>java API</code>来实现数据查询，使用<code>Filters</code>方式查询<br>注释写的很清楚，包含分页，关键字查询，范围查询等等</p>
</blockquote>
<a id="more"></a>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"/testList/&#123;page&#125;"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;Test&gt; <span class="title">list</span><span class="params">(HttpServletRequest request,@PathVariable(<span class="string">"page"</span>)</span> Integer pageNo) <span class="keyword">throws</span> ParseException </span>&#123;</div><div class="line">        <span class="keyword">int</span> pageSize = <span class="number">5</span>;</div><div class="line">        TransportClient client = EsUtil.getTransportClient();</div><div class="line">        SearchRequestBuilder srb = client.prepareSearch().setIndices(<span class="string">"test"</span>).setTypes(<span class="string">"testInfo"</span>);</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * 有两种查询方式，query查询和filter查询</span></div><div class="line"><span class="comment">         * 两者的区别：</span></div><div class="line"><span class="comment">         *          query查询会计算一个分值，es中索引的数据都会存储一个_score分值，分值越高就代表越匹配</span></div><div class="line"><span class="comment">         *          filter查询不会去计算任何分值，也不关心返回的排序问题，因此效率会高一点。</span></div><div class="line"><span class="comment">         *          经常使用过滤器，ES会自动的缓存过滤器的内容，这对于查询来说，会提高很多性能。</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        BoolFilterBuilder boolFilter = FilterBuilders.boolFilter();</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * 通配符查询 查询name中带有 "小" 的</span></div><div class="line"><span class="comment">         * * 匹配任意字符串  ? 匹配任意一个字符</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        boolFilter.must(FilterBuilders.queryFilter(QueryBuilders.wildcardQuery(<span class="string">"name"</span>,<span class="string">"*小*"</span>)));</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * name 查询没有字段或没有值或值为null的数据</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        boolFilter.must(FilterBuilders.missingFilter(<span class="string">"name"</span>));</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * 查询 name 不为null的数据</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        boolFilter.must(FilterBuilders.existsFilter(<span class="string">"name"</span>));</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * idsFilter 查询 testInfo 中 id 为1 和 2 的，</span></div><div class="line"><span class="comment">         * "testInfo"为可变参数</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        boolFilter.must(FilterBuilders.idsFilter(<span class="string">"testInfo"</span>).addIds(<span class="string">"1"</span>,<span class="string">"2"</span>));</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * 查询age等于22 或者 33</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        boolFilter.must(FilterBuilders.inFilter(<span class="string">"age"</span>,<span class="string">"20"</span>,<span class="string">"33"</span>));</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * 查询name 等于 "小明" 的</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        boolFilter.must(FilterBuilders.termFilter(<span class="string">"name"</span>,<span class="string">"小明"</span>));</div><div class="line">        <span class="comment">/**</span></div><div class="line"><span class="comment">         * 查询 createDate 在某个区间内</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        String date1 = <span class="string">"2017-05-04 10:20:00"</span>;</div><div class="line">        String date2 = <span class="string">"2017-05-04 10:30:00"</span>;</div><div class="line">        Date startDate = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>).parse(date1);</div><div class="line">        Date endDate = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>).parse(date2);</div><div class="line">        boolFilter.must(FilterBuilders.rangeFilter(<span class="string">"createDate"</span>).from(startDate.getTime()));</div><div class="line">        boolFilter.must(FilterBuilders.rangeFilter(<span class="string">"createDate"</span>).to(endDate.getTime()));</div><div class="line"></div><div class="line">        SearchResponse searchResponse = srb.setPostFilter(boolFilter)</div><div class="line">                <span class="comment">//pageNo</span></div><div class="line">                .setFrom(pageNo&gt;<span class="number">0</span>?((pageNo-<span class="number">1</span>)*pageSize):<span class="number">0</span>)</div><div class="line">                <span class="comment">//pageSize</span></div><div class="line">                .setSize(pageSize)</div><div class="line">                <span class="comment">//按 id 倒序</span></div><div class="line">                .addSort(<span class="string">"id"</span>, SortOrder.DESC)</div><div class="line">                .execute().actionGet();</div><div class="line">        SearchHits hits = searchResponse.getHits();</div><div class="line">        <span class="comment">//总记录数</span></div><div class="line">        <span class="keyword">long</span> total = hits.getTotalHits();</div><div class="line">        System.out.println(<span class="string">"总记录数："</span> + total);</div><div class="line"></div><div class="line">        SearchHit[] searchHits = hits.hits();</div><div class="line">        List&lt;Test&gt; list = <span class="keyword">new</span> ArrayList&lt;Test&gt;();</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; searchHits.length; i++) &#123;</div><div class="line">            Map&lt;String,Object&gt; map= searchHits[i].getSource();</div><div class="line">            Test t = <span class="keyword">this</span>.recoreEntity(map);</div><div class="line">            list.add(t);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> list;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line"><span class="comment">     * 查询结果映射为对应的实体</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">public</span> Test <span class="title">recoreEntity</span><span class="params">(Map&lt;String,Object&gt; map)</span></span>&#123;</div><div class="line">        Calendar calendar = Calendar.getInstance();</div><div class="line">        Test test = <span class="keyword">new</span> Test();</div><div class="line">        <span class="keyword">for</span> (String key : map.keySet()) &#123;</div><div class="line">            <span class="keyword">if</span>(<span class="string">"id"</span>.equals(key))&#123;</div><div class="line">                test.setId(Long.valueOf(map.get(key).toString()));</div><div class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">"name"</span>.equals(key))&#123;</div><div class="line">                <span class="keyword">if</span>(map.get(key) == <span class="keyword">null</span>)&#123;</div><div class="line">                    test.setName(<span class="keyword">null</span>);</div><div class="line">                &#125;<span class="keyword">else</span>&#123;</div><div class="line">                    test.setName(map.get(key).toString());</div><div class="line">                &#125;</div><div class="line"></div><div class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">"age"</span>.equals(key))&#123;</div><div class="line">                test.setAge(Integer.parseInt((map.get(key).toString())));</div><div class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">"createDate"</span>.equals(key))&#123;</div><div class="line">                <span class="keyword">if</span>(map.get(key) == <span class="keyword">null</span>)&#123;</div><div class="line">                    test.setCreateDate(<span class="keyword">null</span>);</div><div class="line">                &#125;<span class="keyword">else</span> &#123;</div><div class="line">                    calendar.setTimeInMillis(Long.parseLong(map.get(key).toString()));</div><div class="line">                    test.setCreateDate(calendar.getTime());</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> test;</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p><strong>TEST</strong></p>
<blockquote>
<p>查询createDate在 <code>&quot;2017-05-04 10:20:00&quot;</code> 和<code>&quot;2017-05-04 10:30:00&quot;</code>之间的数据<br>pageNo 为1 pageSize 默认5<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"><span class="comment">         * 查询 createDate 在某个区间内</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        String date1 = <span class="string">"2017-05-04 10:20:00"</span>;</div><div class="line">        String date2 = <span class="string">"2017-05-04 10:30:00"</span>;</div><div class="line">        Date startDate = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>).parse(date1);</div><div class="line">        Date endDate = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>).parse(date2);</div><div class="line">        boolFilter.must(FilterBuilders.rangeFilter(<span class="string">"createDate"</span>).from(startDate.getTime()));</div><div class="line">        boolFilter.must(FilterBuilders.rangeFilter(<span class="string">"createDate"</span>).to(endDate.getTime()));</div></pre></td></tr></table></figure></p>
</blockquote>
<p><img src="https://tuchuang001.com/images/2017/05/09/92385187-file_1493974226877_1421e.jpg" alt=""></p>

      
    </div>

    
      
      



      
      
  <div class="post-reward">
    <input type="checkbox" name="reward" id="reward" hidden />
    <label class="reward-button" for="reward">Reward</label>
    <div class="qr-code">
      
      
        <label class="qr-code-image" for="reward">
          <img class="image" src="/image/reward/wechat.png" title="wechat">
        </label>
      
      
        <label class="qr-code-image" for="reward">
          <img class="image" src="/image/reward/alipay.png" title="alipay">
        </label>
      
    </div>
  </div>

    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/elasticsearch/">elasticsearch</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2017/08/17/zhengze/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">正则表达式匹配url</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
      <a class="next" href="/2017/08/17/mysql_elsticsearch/">
        <span class="next-text nav-default">利用mysql触发器创建变更表推送数据到elasticsearch</span>
        <span class="prev-text nav-mobile">Next</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
  </div>


        </div>  
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:ice105ow@qq.com" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
    
    
      <a href="/atom.xml" class="iconfont icon-rss" title="rss"></a>
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  
  <span class="theme-info">
    Theme - 
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>

  <span class="copyright-year">
    
    &copy; 
     
      2016 - 
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">ice</span>
  </span>
</div>
      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  



    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.4.x"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.4.x"></script>

    
  <script type="text/html" id="search-result">
    <article class="post">
      <header class="post-header">
        <h1 class="post-title">
          <a href="$url$" class="post-link">
            $title$
          </a>
        </h1>
      </header>
      <div class="post-content">
        $content$
        <div class="read-more">
          <a href="$url$" class="read-more-link">
            Read more..
          </a>
        </div>
      </div>
    </article>
  </script>
  <script type="text/html" id="no-search-result">
    <div class="no-result">
      <h2>No result found!</h2>
    </div>
  </script>
  <script type="text/javascript" src="/js/src/search.js?v=2.4.x"></script>

  </body>
</html>
